package cc.rengu.igas.bsps.core.service.trans;


import cc.rengu.igas.bsps.common.dao.FundsReconSummaryInfoQueryMapper;
import cc.rengu.igas.bsps.common.dao.impl.FundsReconSummaryInfoQueryMapperImpl;
import cc.rengu.igas.bsps.common.entity.FundCheckStatis;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.MchntSettleSumInfo;
import cc.rengu.igas.bsps.facade.request.FundsReconSummaryInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.FundsReconSummaryInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 资金对账汇总信息查询
 * Created by yuhongwen
 */
public class FundsReconSummaryInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        FundsReconSummaryInfoQueryRequest fundsReconSummaryInfoQueryRequest = new FundsReconSummaryInfoQueryRequest();
        ConvertUtil.convertOutput(fundsReconSummaryInfoQueryRequest);
        return fundsReconSummaryInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        FundsReconSummaryInfoQueryRequest fundsReconSummaryInfoQueryRequest = (FundsReconSummaryInfoQueryRequest) request;
        FundsReconSummaryInfoQueryResponse fundsReconSummaryInfoQueryResponse = new FundsReconSummaryInfoQueryResponse();

        FundsReconSummaryInfoQueryMapper fundsReconSummaryInfoQueryMapper = new FundsReconSummaryInfoQueryMapperImpl();
        List<MchntSettleSumInfo> totalList = new ArrayList<>();

        String instId = fundsReconSummaryInfoQueryRequest.getHeader().getInstId();
        String mchntNo = fundsReconSummaryInfoQueryRequest.getMchntNo();
        String startDate = fundsReconSummaryInfoQueryRequest.getStartDate();
        String endDate = fundsReconSummaryInfoQueryRequest.getEndDate();
        String userId = fundsReconSummaryInfoQueryRequest.getUserId();
        int pageIndex = fundsReconSummaryInfoQueryRequest.getPageIndex();
        int pageSize = fundsReconSummaryInfoQueryRequest.getPageSize();
        rglog.info("必填性校验");
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(startDate)
                || StringUtil.isEmptyOrNull(endDate)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        rglog.info("时间跨度校验，截止时间范围：" + DateUtil.getBaseDateOffsetMonth(startDate, 12));
        if (DateUtil.compareDate(startDate, endDate) > 0 || DateUtil.compareDate(DateUtil.getBaseDateOffsetMonth(startDate, 12), endDate) < 0) {
            rglog.error("时间区域校验失败！");
            throw new BizException(RespCodeEnum.DATESPAN_CHECK_ERROR.getRespCode(), "起止日期范围不能超过一年");
        }
        rglog.info("交易查询开始");
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageIndex(pageIndex);
        /*根据条件查询*/
        PageResult pageResult = fundsReconSummaryInfoQueryMapper.selectFundCheckStatisPage(instId, mchntNo, startDate, endDate, pageInfo);
        if (null == pageResult) {
            rglog.info("根据查询条件查询到0条入账明细!");
            fundsReconSummaryInfoQueryResponse.setTotalRows(0);//总记录数
            fundsReconSummaryInfoQueryResponse.setTotalPage(0);//总页数
        } else {
            rglog.info("交易赋值处理");
            for (Object object : pageResult.getResult()) {
                FundCheckStatis fundCheckStatis = (FundCheckStatis) object;
                MchntSettleSumInfo mchntSettleSumInfoBean = new MchntSettleSumInfo();
                mchntSettleSumInfoBean.setSettleDate(fundCheckStatis.getMchntSettleDate());
                mchntSettleSumInfoBean.setTotalAmt(fundCheckStatis.getTotalTransAmt());
                mchntSettleSumInfoBean.setTotalNum(fundCheckStatis.getTotalTransNum());
                mchntSettleSumInfoBean.setWithdrawalTotalAmt(fundCheckStatis.getCashWithdrawalAmt());
                mchntSettleSumInfoBean.setRealSettleTotalAmt(fundCheckStatis.getRealtimeTransAmt());
                mchntSettleSumInfoBean.setMchntNetAmt(fundCheckStatis.getMchntNetAmt());
                mchntSettleSumInfoBean.setMchntTotalFee(fundCheckStatis.getMchntTotalFee());
                mchntSettleSumInfoBean.setMchntActualFee(fundCheckStatis.getMchntActualFee());
                mchntSettleSumInfoBean.setMchntdiscountFee(fundCheckStatis.getMchntDiscountFee());
                totalList.add(mchntSettleSumInfoBean);
            }
            /* 查询结果赋值 */
            fundsReconSummaryInfoQueryResponse.setPageIndex(pageResult.getPageInfo().getPageIndex());//当前页数
            fundsReconSummaryInfoQueryResponse.setPageSize(pageResult.getPageInfo().getPageSize());  //页显示记录数
            fundsReconSummaryInfoQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());//总记录数
            fundsReconSummaryInfoQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());//总页数
            fundsReconSummaryInfoQueryResponse.setMchntSettleSumInfoList(totalList);
        }

        // 按照接口转换基础产品信息并响应给请求方
        BizResponse<FundsReconSummaryInfoQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(fundsReconSummaryInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        FundsReconSummaryInfoQueryResponse fundsReconSummaryInfoQueryResponse = (FundsReconSummaryInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(fundsReconSummaryInfoQueryResponse);
    }
}

